﻿using Bilgesoft.Enterprise.Server.DomainObjects;
using Bilgesoft.Enterprise.Shared.DataObjects;
using Bilgesoft.Framework.Server.Cache;
using Bilgesoft.Framework.Server.DatabaseLayer;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bilgesoft.Enterprise.Server.BusinessLayer
{
    public class EnterpriseBusinessManagement
    {
        private const string CONNECTION_STRING = "Enterprise";
        private const string CITY_CACHE_KEY = "{61BD97A5-5C06-44BA-B1D0-FDA958CEB90E}";

        private static EnterpriseBusinessManagement _enterprise;
        public static EnterpriseBusinessManagement Current
        {
            get
            {
                if (_enterprise == null)
                    _enterprise = new EnterpriseBusinessManagement();
                return _enterprise;
            }
        }
        private BilgeCache Cache
        {
            get
            {
                return BilgeCache.Current;
            }
        }

        public CityDTO[] GetCitiesById(string id)
        {
            //CityDTO[] cachedCities = Cache.Get<CityDTO[]>(CITY_CACHE_KEY);
            //if (cachedCities != null)
            //    return cachedCities;

            int _id = int.Parse(id);
            using (DatabaseContext db = DatabaseContext.CreateContext(CONNECTION_STRING))
            {
                DbSet<City> cities = db.Set<City>();
                DbSet<Town> towns = db.Set<Town>();

                var queryresult = (from c in cities
                                   where c.Id == _id
                                   join t in towns on c.Id equals t.CityId into townlist
                                   select new
                                   {
                                       c.name,
                                       c.Id,
                                       townlist
                                   }).Take(40).ToArray();

                List<CityDTO> cityresult = new List<CityDTO>();
                foreach (var item in queryresult)
                {
                    CityDTO c = new CityDTO();
                    c.CityName = item.name;
                    c.Citycode = item.Id;
                    c.Towns = (from t in item.townlist
                               select new TownDTO
                               {
                                   Name = t.Name,
                                   TownId = t.Id
                               }).ToArray();
                    cityresult.Add(c);
                }
               // cachedCities = cityresult.ToArray();
                //Cache.Put<CityDTO[]>(CITY_CACHE_KEY, cachedCities);

                return cityresult.ToArray();
            }
        }
        public CityDTO[] GetCities()
        { 
            CityDTO[] cachedCities = Cache.Get<CityDTO[]>(CITY_CACHE_KEY);
                if (cachedCities != null)
                    return cachedCities;
            using (DatabaseContext db = DatabaseContext.CreateContext(CONNECTION_STRING))
            {
                DbSet<City> cities = db.Set<City>();
                DbSet<Town> towns = db.Set<Town>();

                var queryresult = (from c in cities
                                   join t in towns on c.Id equals t.CityId into townlist
                                   select new
                                   {
                                       c.name,
                                       c.Id,
                                       townlist
                                   }).Take(40).ToArray();

                List<CityDTO> cityresult = new List<CityDTO>();
                foreach (var item in queryresult)
                {
                    CityDTO c = new CityDTO();
                    c.CityName = item.name;
                    c.Citycode = item.Id;
                    c.Towns = (from t in item.townlist
                               select new TownDTO
                               {
                                   Name = t.Name,
                                   TownId = t.Id
                               }).ToArray();
                    cityresult.Add(c);
                }
                cachedCities =  cityresult.ToArray();
                Cache.Put<CityDTO[]>(CITY_CACHE_KEY, cachedCities);

                return cachedCities;
            }
        }

    }
}
